Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pdf-lib

Package Overview
Dependencies
Maintainers
1
Versions
84
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pdf-lib

Create and modify PDF files with JavaScript

  • 1.17.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1M
decreased by-3.91%
Maintainers
1
Weekly downloads
 
Created

What is pdf-lib?

pdf-lib is a JavaScript library that allows you to create, modify, and extract information from PDF documents. It is a powerful tool for handling PDF files in a web or Node.js environment.

What are pdf-lib's main functionalities?

Create a new PDF document

This feature allows you to create a new PDF document from scratch. The code sample demonstrates how to create a new PDF document, add a page to it, draw some text on the page, and then save the document.

const { PDFDocument } = require('pdf-lib');

async function createPdf() {
  const pdfDoc = await PDFDocument.create();
  const page = pdfDoc.addPage([600, 400]);
  page.drawText('Hello, world!', { x: 50, y: 350 });
  const pdfBytes = await pdfDoc.save();
  return pdfBytes;
}

createPdf().then((pdfBytes) => {
  // Do something with the PDF bytes
});

Modify an existing PDF document

This feature allows you to modify an existing PDF document. The code sample demonstrates how to load an existing PDF, add some text to the first page, and then save the modified document.

const { PDFDocument } = require('pdf-lib');
const fs = require('fs');

async function modifyPdf() {
  const existingPdfBytes = fs.readFileSync('existing.pdf');
  const pdfDoc = await PDFDocument.load(existingPdfBytes);
  const pages = pdfDoc.getPages();
  const firstPage = pages[0];
  firstPage.drawText('This is an added text!', { x: 50, y: 500 });
  const pdfBytes = await pdfDoc.save();
  fs.writeFileSync('modified.pdf', pdfBytes);
}

modifyPdf();

Extract text from a PDF document

This feature allows you to extract text from a PDF document. The code sample demonstrates how to load an existing PDF, get the text content from the first page, and log it to the console.

const { PDFDocument } = require('pdf-lib');
const fs = require('fs');

async function extractText() {
  const existingPdfBytes = fs.readFileSync('existing.pdf');
  const pdfDoc = await PDFDocument.load(existingPdfBytes);
  const pages = pdfDoc.getPages();
  const firstPage = pages[0];
  const textContent = firstPage.getTextContent();
  console.log(textContent);
}

extractText();

Other packages similar to pdf-lib

Keywords

FAQs

Package last updated on 06 Nov 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc